﻿<rules>
  <rule id="Common.Core.1009" category="core" target="error" specificationsection ="2.2.8.1.1" requirementlevel="must" format="xml" metadata="true">
    <description>XML Error Response format MUST be used in response payloads representing top-level errors.</description>
    <action processor="xslt+rng">
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
        <xsl:template match="/">
          <grammar xmlns="http://relaxng.org/ns/structure/1.0">
            <xsl:apply-templates select="*[local-name()='Edmx']"/>
            <start>
              <element name="error">
                <ref name="anyAttribute" />
                <interleave>
                  <element name="code">
                    <text/>
                  </element>
                  <element name="message">
                    <ref name="anyAttribute" />
                    <ref name="anyContent" />
                  </element>
                  <zeroOrMore>
                    <element name="innererror">
                      <ref name="anyAttribute" />
                      <ref name="anyContent" />
                    </element>
                  </zeroOrMore>
                </interleave>
              </element>
            </start>
            <define name="anyElement">
              <element>
                <anyName />
                <zeroOrMore>
                  <choice>
                    <attribute>
                      <anyName />
                    </attribute>
                    <text />
                    <ref name="anyElement" />
                  </choice>
                </zeroOrMore>
              </element>
            </define>
            <define name="anyAttribute">
              <zeroOrMore>
                <choice>
                  <attribute>
                    <anyName />
                  </attribute>
                </choice>
              </zeroOrMore>
            </define>
            <define name="anyContent">
              <zeroOrMore>
                <choice>
                  <attribute>
                    <anyName />
                  </attribute>
                  <text />
                  <ref name="anyElement" />
                </choice>
              </zeroOrMore>
            </define>
          </grammar>
        </xsl:template>
        <xsl:template name="make-namespace-node-other-than">
          <xsl:param name="prefix"/>
          <xsl:param name="uri"/>
          <xsl:attribute name="{ $prefix }">
            <xsl:value-of select="$uri"/>
          </xsl:attribute>
        </xsl:template>
        <xsl:template match="*[local-name()='Edmx']" xmlns="http://relaxng.org/ns/structure/1.0">
          <xsl:call-template name="make-namespace-node-other-than">
            <xsl:with-param name="prefix" select="'ns'" />
            <xsl:with-param name="uri" select="string(//*/namespace::m)" />
          </xsl:call-template>
        </xsl:template>
      </xsl:stylesheet>
    </action>
    <errormessage>XML Error Response format MUST be used in response payloads representing top-level errors.</errormessage>
  </rule>
</rules>